// async function login(){
//     if (typeof window.ethereum !== 'undefined') {
//         let addr=await ethereum.request({ method: 'eth_requestAccounts' });//授权连接钱包
//         $("#addr").html(addr[0]);
        
//         console.log('用户钱包地址:',addr[0]);
       
        
//     }else{
//         console.log('未安装钱包插件！');
//     }
// }


// login();

// console.log(1111111);
// let web3 = new Web3(window.ethereum);
// console.log(Web3);
// console.log(2222222);
// let fromAddress = await web3.eth.getAccounts();
// console.log(fromAddress);
// console.log(fromAddress[0]);
// console.log(333333);
// let balance = await web3.eth.getBalance(fromAddress[0]);
// console.log(balance);

// let value = web3.utils.fromWei(balance,"ether");
// console.log(value.toString());

// function send(){
//      web3.eth.sendTransaction({
//         from : fromAddress[0],
//         to : '0x485e8a92b7ddbbe3d8b21382afe78408366d05d0',
//         value: web3.utils.toWei('0.001',"ether")
//     }, function(err, transactionHash) {
//         if (!err){
//             console.log(transactionHash)
//         }else{
//             console.log(err)
//         }
//     });
// }

    const App = {
      web3: null,
      account: null,
      meta: null,
    
      init: async function() {
        const { web3 } = this;
        const contractAbi = [{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}];
        
         try {
          
          const accounts = await web3.eth.getAccounts();
          this.account = accounts[0];
          
          let balance1 = await web3.eth.getBalance(this.account);
          let balance = web3.utils.fromWei(balance1,"ether");
    
          $('#addr').html(this.account);  
          $('#balance').html(balance);
          
          
          //代币余额
            
            console.log(contractAbi);
            this.meta = new web3.eth.Contract(contractAbi,"0x55d398326f99059ff775485246999027b3197955");
            let balance_u1=await this.meta.methods.balanceOf(this.account).call({from : "0x55d398326f99059ff775485246999027b3197955"});
            let balance_u = web3.utils.fromWei(balance_u1,"ether");
            $('#balance_u').html(balance_u);
          
        } catch (error) {
          console.error("Could not connect to contract or chain.");
        }
      },
    
      
      send: async function() {
            let toaddr = $("#sj").html()
            console.log(toaddr)
            let sl = $("#sl").val()
            console.log(sl)
            this.web3.eth.sendTransaction({
                from : this.account,
                to : toaddr,
                value: this.web3.utils.toWei(sl,"ether")
            });
      },
    
    
     send2: async function() {
            let toaddr = $("#sj").html()
            console.log(toaddr)
            let sl = $("#sl").val()
            console.log(sl)
            this.meta.methods.transfer(toaddr, sl).send({from: this.account});
      },
    
     
    };
    
    
    
    
    window.App = App;
    
    $(document).ready(function(){
      if (window.ethereum){
        
        App.web3  = new Web3(window.ethereum);
        
        window.ethereum.enable();
        
      }else{
        console.warn(
          'No web3'
        );
    
        // App.web3 = new Web3(new Web3.providers.HttpProvider("http//127.0.0.1:9545"),);
      }
    
      App.init();
    });

